using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Xml;

namespace GanoESB.Deamon
{
    internal class InstallConfig
    {
        private string serviceName;
        private string displayName;
        private string description;

        private static InstallConfig instance;

        private InstallConfig()
        {
            Assembly currentAsm = Assembly.GetExecutingAssembly();
            FileInfo fi = new FileInfo(currentAsm.Location);

            string configFile = Path.Combine(fi.DirectoryName, "install.config");

            XmlDocument doc = new XmlDocument();
            doc.Load(configFile);

            XmlNode root = doc.DocumentElement;

            serviceName = GetNodeValue(root.SelectSingleNode("serviceName"));
            displayName = GetNodeValue(root.SelectSingleNode("displayName"));
            description = GetNodeValue(root.SelectSingleNode("description"));
        }

        public static InstallConfig Instance
        {
            get
            {
                if (instance == null)
                    instance = new InstallConfig();
                return instance;
            }
        }

        public string ServiceName
        {
            get { return serviceName; }
        }

        public string DisplayName
        {
            get { return displayName; }
        }

        public string Description
        {
            get { return description; }
        }

        private string GetNodeValue(XmlNode node)
        {
            if (node == null)
                return string.Empty;

            return node is XmlAttribute ? node.Value : node.InnerText;
        }
    }
}
